package test;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringPractice {
    @Test
    public void test1() throws UnsupportedEncodingException {
//        String s = "hello";
//        String s1 = new String("hello");
//        s1 = s;
//        char[] a = {'g','o','o','d'};
////        String s = new String(a,1,);
////        String s = "";
//        String s = "hello";
//        String s1 = new String("hello");
//        System.out.println(s == s1);
//
//
////        System.out.println(s1.hashCode());
//        System.out.println(s.hashCode());
//        System.out.println(s1.hashCode());
//        byte[] b = {-60, -29, -70, -61};
//        String s = new String(b, "gbk");
//        System.out.println(s);
        char[] ch = new char["hello".length()];
        "hello".getChars(0, 5, ch, 0);
        System.out.println(ch);
    }

    @Test
    public void test02() {
        String str1 = "hello world";
//        System.out.println(str.lastIndexOf(""));
//        System.out.println(str.length());
        String str2 = "oro";

        if (check(str1, str2)) {
            System.out.println("符合要求");
        } else {
            System.out.println("不符合要求");
        }


    }

    @Test
    public void test03() {
//        String str = "Hllloworld";
//        String upperCase = str.toLowerCase();
//        if(str.equalsIgnoreCase(upperCase)){
//            System.out.println("相同");
//        }
//        if(str.compareTo(upperCase) > 0){
//            System.out.println(str + "大");
//        }else{
//            System.out.println(str + "小");
//        }
//        if(str.length() % 2 != 0){
//            System.out.println(str.charAt(str.length()/2));
//        }else{
//            System.out.println(str.charAt(str.length()/2 - 1));
//        }
//        System.out.println(str.substring(1));
//        System.out.println(str.length());
//        System.out.println(str.trim().length());
//        if(str.startsWith("he")){
//            System.out.println("以he开头");
//        }
//        if(str.endsWith(" ")){
//            System.out.println("以空格结尾");
//        }

    }

    @Test
    public void test04() {
        String email = "sea1415926@163.com@qq.com";
        String[] split = email.split("@");
        for (String s : split) {
            System.out.println(s);
        }
    }

    public static boolean checkOnly(String str1, String str2) {
        int length1 = str1.length();
        int length2 = str2.length();
        int count = 0;
        int index = 0;
        char[] ch = new char[length2];
        str2.getChars(0, length2, ch, 0);
        for (int i = 0; i < length1; i++) {
            if (ch[0] == str1.charAt(i)) {
                for (int j = 1; j < length2; j++) {
                    if (ch[j] == str1.charAt(i + j)) {
                        index++;
                    }
                }
                if (index == length2 - 1) {
                    count++;
                }
                index = 0;
            }
        }
        return count == 1;
    }

    @Test
    public void test05() throws IOException {
        FileInputStream fis = new FileInputStream("D:\\IDEA\\java资料\\TM（实例源码+习题答案）\\2. TM（实例源码+习题答案）\\10\\编程训练\\13\\punctuation.java");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "gbk"));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
    @Test
    public void test06() {
        Date date = new Date();
//        System.out.printf("%te%n",date);
        String year = String.format("%tY", date);
        String month = String.format("%tB", date);
        String day = String.format("%td", date);
        System.out.println("今年是" + year);
        System.out.println("本月是" + month);
        System.out.println("今天是" + day);
        System.out.printf("%tY-%tm-%td %tH:%tM:%tS%n",date,date,date,date,date,date);
        System.out.println(String.format("%tF", date));
        System.out.println(String.format("%tc", date));
        System.out.println(String.format("%tD", date));
        System.out.println(String.format("%tT", date));
        System.out.printf("%tR", date);
        System.out.printf("%d%n",400 / 2);
        System.out.printf("%b%n",3 > 5);
        System.out.printf("%x%n",200);
        System.out.printf("%%%d%n",50);
        System.out.printf("%tI:%tM:%tS %tp%n",date,date,date,date);
        System.out.printf("玩家%d连续完成%d次击杀后，获得了%s的称号",1,5,"已超神");
    }

    @Test
    public void test07()  {
//        Date date = new Date();
//        System.out.printf("HH:MM:SS PM格式（12时制）：%tr%n", date);
        String rex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
        String rex2 = "[a-zA-Z]";
        String content = "1111@111ffyu.dfg.com合法";
        String str1 = "aaa@";
        String str2 = "aaaaa";
        String str3 = "1111@111ffyu.dfg.com";
//        if(str1.matches(rex)){
//            System.out.println(str1+ "合法");
//        }
//        if(str2.matches(rex)){
//            System.out.println(str2+ "合法");
//        }
//        if(str3.matches(rex)){
//            System.out.println(str3+ "合法");
//        }
//        Pattern compile = Pattern.compile(rex2, Pattern.CASE_INSENSITIVE);
//        Matcher matcher = compile.matcher(content);
//        while(matcher.find()){
//            System.out.println(matcher.group(0));
//        }

    }

    @Test
    public void test08() {
//        String str = "";
//        long l1 = System.currentTimeMillis();
//        for (int i = 0; i < 10000; i++) {
//            str += i;
//        }
//        long l2 = System.currentTimeMillis();
//        System.out.println(l2 - l1);
//        l1 = System.currentTimeMillis();
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i = 0; i < 10000; i++) {
//            stringBuilder.append(i);
//        }
//        l2 = System.currentTimeMillis();
//        System.out.println(l2 - l1);
        String str = "熊出没小心";
        StringBuilder stringBuilder = new StringBuilder(str);
//        stringBuilder.append(true);
//        System.out.println(stringBuilder.toString());
//        stringBuilder.insert(1,100);
//        System.out.println(stringBuilder.toString());
//        stringBuilder.replace(3, 7, "****");
        stringBuilder.insert(3,",");
        stringBuilder.insert(6,";");
        stringBuilder.append(str);
        stringBuilder.insert(9,",");

        System.out.println(stringBuilder);


    }

    public static boolean check(String original, String substr) {
        if (original.contains(substr)) {
            return original.indexOf(substr) == original.lastIndexOf(substr);
        } else {
            return false;
        }
    }
}
